package com.sky.controller.user;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.ShoppingCartDTO;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import com.sky.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("user/shoppingCart")
@Api(tags = "购物车接口")
@RestController
public class UserShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     *查看购物车
     * @return
     */
    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<com.sky.entity.ShoppingCart>> GetShoppingCartlist(){
        return Result.success(shoppingCartService.GetShoppingCartlist());
    }

    /**
     *添加购物车
     */
    @ApiOperation("添加购物车")
    @PostMapping("/add")
    public Result AddShoppingCart(@RequestHeader("authentication") String authorizationHeader,
                                  @RequestBody ShoppingCartDTO shoppingCartDTO) {


        String token = authorizationHeader.replace("Bearer ", "");
        Long userId;

        JwtProperties jwtProperties  = new JwtProperties();
        try {
            // 解析 JWT，以获取用户信息
            Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);  // 解析 JWT
            userId = claims.get(JwtClaimsConstant.USER_ID, Long.class);
        } catch (Exception e) {
            // 返回错误处理的信息，例如 Token 无效、解析失败等
            return Result.error("无效的 token: " + e.getMessage());
        }

        // 进行添加购物车的逻辑，传入 userId
        shoppingCartService.AddShoppingCart(shoppingCartDTO, userId);

        return Result.success("购物车添加成功");
    }

    /**
     *删除购物车中一个商品
     */
    @ApiOperation("删除购物车中一个商品")
    @PostMapping("/sub")
    public Result SubShoppingCart(@RequestHeader("authentication") String authorizationHeader,
                                  @RequestBody ShoppingCartDTO shoppingCartDTO) {


        String token = authorizationHeader.replace("Bearer ", "");
        Long userId;

        JwtProperties jwtProperties = new JwtProperties();
        try {
            // 解析 JWT，以获取用户信息
            Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);  // 解析 JWT
            userId = claims.get(JwtClaimsConstant.USER_ID, Long.class);
        } catch (Exception e) {
            // 返回错误处理的信息，例如 Token 无效、解析失败等
            return Result.error("无效的 token: " + e.getMessage());
        }

        // 进行添加购物车的逻辑，传入 userId
        shoppingCartService.SubShoppingCart(shoppingCartDTO, userId);

        return Result.success("删除成功");
    }



    /**
     *清空购物车
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result CleanShoppingCart(@RequestHeader("authentication") String authorizationHeader){

        String token = authorizationHeader.replace("Bearer ", "");
        Long userId;

        JwtProperties jwtProperties = new JwtProperties();
        try {
            // 解析 JWT，以获取用户信息
            Claims claims = JwtUtil.parseJWT(jwtProperties.getUserSecretKey(), token);  // 解析 JWT
            userId = claims.get(JwtClaimsConstant.USER_ID, Long.class);
        } catch (Exception e) {
            // 返回错误处理的信息，例如 Token 无效、解析失败等
            return Result.error("无效的 token: " + e.getMessage());
        }

        shoppingCartService.CleanShoppingCart(userId);
        return Result.success();
    }
}
